﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using dhDataAcess.produceDAL;
using dhSystemBLL.produceBLL;
using dhSystemBLL.orderData;
using dhDataAcess.produceLinq;
using baseSet.importExport;
using dhDataAcess.baseBLL;

namespace dhsystem.produceForm
{
    public partial class FrmcgddDetailAdd : Form
    {
        cgddBLL cgdd = new cgddBLL();
        cgddDetailModel cgddDetailRow = new cgddDetailModel();
        materialListBLL materialList = new materialListBLL();
        BindingSource b2;
        string state;
        string billNo;
        //绑定数据源
        public FrmcgddDetailAdd(BindingSource b1, string _state, string _billNo)
        {
            InitializeComponent();
            b2  = b1 ;
            state = _state;
            billNo = _billNo;
        }
        //打开窗口时绑定数据
        private void FrmcgddDetailAdd_Load(object sender, EventArgs e)
        {
            //bindingSourcematerialList.DataSource = materialList.getmaterialListAll();
            //comboBoxmatNum.DataSource = bindingSourcematerialList;
            //comboBoxmatNum.DisplayMember = "matName";
            //comboBoxmatNum.ValueMember = "matNum";

            if (state == "编辑")
            {
                textbillNo.DataBindings.Add("text", b2, "billNo");
                textmatNum.DataBindings.Add("text", b2, "matNum");
                textmatName.DataBindings.Add("text", b2, "matName");
                textmatSpec.DataBindings.Add("text", b2, "matSpec");
                textqty.DataBindings.Add("text", b2, "qty");
                textunit.DataBindings.Add("text", b2, "unit");
                textprice.DataBindings.Add("text", b2, "price ");
                textacount.DataBindings.Add("text", b2, "acount");
                comboBoxcurrency.DataBindings.Add("text", b2, "currency");
                textfqty.DataBindings.Add("text", b2, "fqty ");
                textfunit.DataBindings.Add("text", b2, "funit");
                textmark.DataBindings.Add("text", b2, "mark");

                //textid.DataBindings.Add("text", BindingSourcescrlDetailAdd, "recordid");
                // BindingSourceDetailAdd.AddNew();
                //textproductname.Text = productname;
            }
            if (state == "新增")
            {
               // textbillNo.Text  = billNo;
                textbillNo .DataBindings .Add ("text",b2, "billNo");
                textmatNum.DataBindings.Add("text", b2, "matNum");
                textmatName.DataBindings.Add("text", b2, "matName");
                textmatSpec.DataBindings.Add("text", b2, "matSpec");
                textqty.DataBindings.Add("text", b2, "qty");
                textunit.DataBindings.Add("text", b2, "unit");
                textprice.DataBindings.Add("text", b2, "price");
                textacount.DataBindings.Add("text", b2, "acount");
                textfqty.DataBindings.Add("text", b2, "fqty");
                comboBoxcurrency.DataBindings.Add("text", b2, "currency");
                textmark.DataBindings.Add("text", b2, "mark");
                b2.AddNew();
                textbillNo.Text = billNo;
            }

            textmatSpec.Focus();
        }
        //保存按钮，插入或修改数据
        private void buttonOK_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult result = MessageBox.Show("是否保存资料！", "提示信息", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {

                    b2.EndEdit(); //编辑到完成，写到数据源
                    b2.CancelEdit(); //刷新数据作用

                }
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
        }
        //取消按钮
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            b2.CancelEdit();
            this.Close();
        }
        //选择原材料名
        private void buttselectpname_Click(object sender, EventArgs e)
        {
            string field;
            field = "matNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.materialList , field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textmatNum.Text  = productselect.pname;
            }
        }


        private void textprice_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textqty.Text != "" && textprice.Text != "")
                {
                    textacount.Text = (Convert.ToDecimal(textqty.Text) * Convert.ToDecimal(textprice.Text)).ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }

        private void textqty_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (textqty.Text != "" && textprice.Text != "")
                {
                    textacount.Text = (Convert.ToDecimal(textqty.Text) * Convert.ToDecimal(textprice.Text)).ToString();
                }
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        //选择原材料
        private void buttworkerselect_Click(object sender, EventArgs e)
        {
            string field;
            field = "matNum";
            Frmprodutselect productselect = new Frmprodutselect(moduleInfo.materialList, field);
            DialogResult result = productselect.ShowDialog();
            if (result == DialogResult.Yes)
            {
                textmatNum.Text = productselect.pname;
            }
        }

        private void textmatNum_TextChanged(object sender, EventArgs e)
        {
            try
            {
                textunit.Text = basicSql.getunitbymaterialList(textmatNum.Text);
                textmatSpec.Text = basicSql.getmatSpecbymaterialList(textmatNum.Text);
                textmatName.Text = basicSql.getmatNamebymaterialList(textmatNum.Text);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
    }
}
